/*
 * Copyright (c) 2020 cloud4cloud Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bigbirdmed.cloud.admin.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigbirdmed.cloud.admin.api.dto.SysLogDTO;
import com.bigbirdmed.cloud.admin.api.entity.SysLog;
import com.bigbirdmed.cloud.admin.api.vo.LogVO;
import com.bigbirdmed.cloud.admin.mapper.SysLogMapper;
import com.bigbirdmed.cloud.admin.service.SysLogService;
import com.bigbirdmed.cloud.common.core.constant.enums.DelFlagEnum;
import com.bigbirdmed.cloud.common.core.util.PageUtil;
import com.bigbirdmed.cloud.common.core.util.Pagination;
import com.bigbirdmed.cloud.common.core.util.R;
import com.bigbirdmed.cloud.common.security.util.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 日志表 服务实现类
 * </p>
 *
 * @author lengleng
 * @since 2019/2/1
 */
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements SysLogService {
	@Autowired
	private SysLogMapper sysLogMapper;

	@Override
	public R<Pagination<LogVO.LogPageVO>> LogPage(SysLogDTO.LogPageDTO dto){
		Pagination<LogVO.LogPageVO> pagination = new Pagination<>(dto.getPageNum(), dto.getPageSize());
		Page<Long> page = PageUtil.getPage(dto);
		Long total = sysLogMapper.countLogPage(dto);
		PageUtil.resetPageCurrent(total,page);
		List<LogVO.LogPageVO> pageVOS = sysLogMapper.LogPage(page, dto);
		pagination.setRows(pageVOS);
		pagination.setTotal(total);
		return R.ok(pagination);
	}

	@Override
	public R<Void> removeByIds(LogVO.RemoveByIdsDTO dto){
		ArrayList<SysLog> list = new ArrayList<>();
		dto.getRows().forEach(x ->{
			SysLog sysLog = new SysLog();
			sysLog.setId(x.getId());
			sysLog.setDelFlag(DelFlagEnum.DELETED.getCode());
			sysLog.setUpdateTime(LocalDateTime.now());
			sysLog.setUpdateBy(SecurityUtils.getAuthentication().getName());
			list.add(sysLog);
		});
		updateBatchById(list);
		return R.ok("修改成功");
	}

	@Override
	public R<Void> SaveLog(SysLogDTO.SaveLogDTO dto){
		SysLog sysLog = new SysLog();
		BeanUtils.copyProperties(dto,sysLog);
		sysLog.setCreateTime(LocalDateTime.now());
		sysLog.setCreateBy(SecurityUtils.getAuthentication().getName());
		sysLog.setDelFlag(DelFlagEnum.NOT_DELETED.getCode());
		save(sysLog);
		return R.ok("新增成功");
	}

	@Override
	public List<LogVO.LogPageVO> exportLog(SysLogDTO.LogPageDTO dto) {
		return sysLogMapper.exportLog(dto);
	}
//	/**
//	 * 列表查询日志
//	 * @param sysLog 查询条件
//	 * @return List
//	 */
//	@Override
//	public List getLogList(SysLogDTO sysLog) {
//		return baseMapper.selectList(buildQueryWrapper(sysLog));
//	}

//	/**
//	 * 构建查询的 wrapper
//	 * @param sysLog 查询条件
//	 * @return LambdaQueryWrapper
//	 */
//	private LambdaQueryWrapper buildQueryWrapper(SysLogDTO sysLog) {
//		LambdaQueryWrapper<SysLog> wrapper = Wrappers.lambdaQuery();
//		if (StrUtil.isNotBlank(sysLog.getType())) {
//			wrapper.eq(SysLog::getType, sysLog.getType());
//		}
//
//		if (ArrayUtil.isNotEmpty(sysLog.getCreateTime())) {
//			wrapper.ge(SysLog::getCreateTime, sysLog.getCreateTime()[0]).le(SysLog::getCreateTime,
//					sysLog.getCreateTime()[1]);
//		}
//
//		return wrapper;
//	}

}
